package search;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;

public class TwitterSearcher {
	private IndexSearcher m_searcher;
	private int m_maxNum = 10000;
	private IndexReader m_reader;

	public TwitterSearcher(File indexDir){
		try {
			m_searcher = new IndexSearcher(indexDir.toString());
			m_reader = IndexReader.open(indexDir);
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	public List<TwitterRecord> searchRecords(String queryText){
		List<TwitterRecord> records = new Vector<TwitterRecord>();
		QueryParser parser = new QueryParser("text", new StandardAnalyzer());
		try {
			Query query = parser.parse(queryText);
			TopDocs docs = m_searcher.search(query, m_maxNum);
			ScoreDoc[] docArray = docs.scoreDocs;
			for(int i = 0 ; i < docArray.length ; i ++){
				Document doc = m_reader.document(docArray[i].doc);
				String id = doc.getField("id").stringValue();
				String timeText = doc.getField("time").stringValue();
				String user = doc.getField("user").stringValue();
				String text = doc.getField("text").stringValue();
				records.add(new TwitterRecord(id, user, timeText, text));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return records;
	}
	
	public List<String> searchId(String queryText){
		List<String> ids = new Vector<String>();
		QueryParser parser = new QueryParser("text", new StandardAnalyzer());
		try {
			Query query = parser.parse(queryText);
			TopDocs docs = m_searcher.search(query, m_maxNum);
			ScoreDoc[] docArray = docs.scoreDocs;
			for(int i = 0 ; i < docArray.length ; i ++){
				Document doc = m_reader.document(docArray[i].doc);
				String id = doc.getField("id").stringValue();
				ids.add(id);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ids;
	}
	
	public TwitterRecord getRecord(String id){
		Query query = new TermQuery(new Term("id", id));
		try {
			TopDocs docs = m_searcher.search(query, 1);
			if(docs.totalHits == 0){
				return null;
			}
			Document doc = m_reader.document(docs.scoreDocs[0].doc);
			String timeText = doc.getField("time").stringValue();
			String user = doc.getField("user").stringValue();
			String text = doc.getField("text").stringValue();
			return new TwitterRecord(id, user, timeText, text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args){
		int argIndex = 0;
		File indexDir = new File(args[argIndex++]);
		TwitterSearcher searcher = new TwitterSearcher(indexDir);
		List<String> ids = searcher.searchId("gasp");
		for(String id: ids){
			System.out.println(id);
		}
		TwitterRecord record = searcher.getRecord("2599121832");
		System.out.println(record.m_id);
	}
}
